﻿; (function () {

    'use strict';

    function computeRegions() {
        var regions = [];

        for (var i = 0; i < 9; i++) {
            var squareRow = (i / 3) | 0;
            regions[i] = [];
            for (var j = 0; j < 9; j++) {
                var squareCol = (j / 3) | 0;
                var regionId = (squareRow * 3) + squareCol;
                regions[i][j] = regionId;
            }
        }
        return regions;
    }

    window.RegionHelper = window.RegionHelper || {};

    RegionHelper.computedRegions = computeRegions();

    RegionHelper.getRegionIdOrNull = function (cells) {
        var referenceRegionId = null;
        for (var i = 0, len = cells.length; i < len; i++) {
            var cell = cells[i];
            var regionId = RegionHelper.computedRegions[cell.indices.i][cell.indices.j];
            if (i === 0) {
                referenceRegionId = regionId
            }
            else if (referenceRegionId !== regionId) {
                return null;
            }
        }
        return referenceRegionId;
    }

    RegionHelper.getAlignedRegions = function (regionId, alignMode) {
        if (alignMode == "row") {
            if (regionId == 0 || regionId == 1 || regionId == 2)
                return [0, 1, 2];
            if (regionId == 3 || regionId == 4 || regionId == 5)
                return [3, 4, 5];
            if (regionId == 6 || regionId == 7 || regionId == 8)
                return [6, 7, 8];
        }

        if (alignMode == "column") {
            if (regionId == 0 || regionId == 3 || regionId == 6)
                return [0, 3, 6];
            if (regionId == 1 || regionId == 4 || regionId == 7)
                return [1, 4, 7];
            if (regionId == 2 || regionId == 5 || regionId == 8)
                return [2, 5, 8];
        }
    }


}(window));
